package com.sun.umbrella.service.oauth2.impl;

import com.sun.umbrella.service.oauth2.ClientService;
import com.sun.umbrella.service.oauth2.OauthService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;

/**
 * @author Jay
 * @version v1.0
 * @description 请添加类描述
 * @date 2017-11-24 11:20
 */

@Service
@Slf4j
public class OauthServiceImpl implements OauthService {


    private Cache cache;

    @Autowired
    private ClientService clientService;

    @Autowired
    public OauthServiceImpl(CacheManager cacheManager) {
        this.cache = cacheManager.getCache("code-cache");
    }

    @Override
    public void addAuthCode(String authCode, String username) {
        cache.put(authCode, username);
    }

    @Override
    public void addAccessToken(String accessToken, String username) {
        cache.put(accessToken, username);
    }

    @Override
    public String getUsernameByAuthCode(String authCode) {
        return (String) cache.get(authCode).get();
    }

    @Override
    public String getUserNameByAccessToken(String accessToken) {
        return (String) cache.get(accessToken).get();
    }

    @Override
    public Boolean checkAuthCode(String authCode) {
        return cache.get(authCode) != null;
    }

    @Override
    public Boolean checkAccessToken(String accessToken) {
        return cache.get(accessToken) != null;
    }

    @Override
    public Boolean checkClientId(String clientId) {
        return clientService.findByClientId(clientId) != null;
    }

    @Override
    public Boolean checkClientSecret(String clientSecret) {
        return clientService.findByClientSecret(clientSecret) != null;
    }

    @Override
    public Long getExpireIn() {
        return 3600L;
    }


}
